package idx;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Hashtable;


public class Extractor {
	Hashtable<Character, Character> convertTable;
	public Extractor()
	{
		convertTable = new Hashtable<Character, Character>();
	}
	public static void main(String []args) throws IOException
	{
		Extractor a = new Extractor();
		a.buildConvertTable("table.txt");
		a.recursiveAdd(args[0]);
		return;
	}
	void recursiveAdd(String path) throws IOException
	{
		File f = new File(path);
		//System.out.println(path);
		File sub[] = f.listFiles();
		StringBean bean = new StringBean();
        bean.setLinks (false);
        bean.setReplaceNonBreakingSpaces (true);
        bean.setCollapse (true);
        System.out.println(sub[sub.length-1].getName());
        /*
		for(int i = 0; i < sub.length; i++)
		{
			//if(i % (sub.length / 100) == 0)
			//System.out.println(sub[i].getAbsolutePath());
			if(sub[i].isDirectory())
				recursiveAdd(sub[i].getAbsolutePath());
			else
			{
				boolean done = false;
				if(!sub[i].getName().endsWith(".html"))
					continue;
				for(int j = 0; j < sub.length; j++)
				{
					if((sub[i].getName() + ".ext").equals(sub[j].getName()))
					{
						done = true;
						break;
					}
				}
				if(done)
					continue;
				System.out.println(sub[i].getName());
				BufferedReader reader = new BufferedReader(new FileReader(sub[i]));
				StringBuilder context = new StringBuilder();
				String line = "";
				while((line = reader.readLine()) != null)
					context.append(line + "\n");
				bean.setURL(context.toString());
				FileWriter w = new FileWriter(sub[i].getAbsolutePath() + ".ext");
				//String result = Convert(bean.getStrings());
				//System.out.println(result);
				w.write(Convert(bean.getStrings()));
				w.close();
			}
		}*/
		return;
	}
	public void buildConvertTable(String path) throws IOException
	{
		BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
		String line = "";
		while ((line = reader.readLine()) != null)
		{
			String [] grp = line.split("　　");
			for(int i = 0; i < grp.length; i++)
			{
				String [] words = grp[i].split("，");
				//System.out.println(words[0].charAt(words[0].length() - 1)+ " " + words[1].charAt(words[1].length() - 1));
				convertTable.put(words[1].charAt(words[1].length() - 1), words[0].charAt(words[0].length() - 1));				
			}
			//System.out.println(line + grp.length);
		}
	}
	public String Convert(String context)
	{
		char []words = context.toCharArray();
		//System.out.println(words);
		for(int i = 0; i < words.length; i++)
		{
			if(convertTable.containsKey(words[i]))
			{
				//System.out.println(words[i]);
				words[i] = convertTable.get(words[i]);
			}
			
		}
		//System.out.println(words.toString());
		return new String(words);
	}
}
